import MetaUtils from "@/utils/MetaUtils";

function getOptions(model) {
    let dataType = model['dataType'];
    if(dataType == 'dict') {
        return ` dict="${model['dictValue']}"`
    } else {
        return ` url="${model['urlValue']}"`
    }
}
function isBoolean(val) {
    return val == 'true' || val == 'false'
}
function text(meta, defaultValue) {
    let model = meta['tabModel'];
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzInput field="%s" title="%s"${defaultValue}${clear}${extra}${placeholder} />`;
}
function radio(meta, defaultValue) {
    let model = meta['tabModel'];
    let options = getOptions(model);
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzRadio field="%s" title="%s"${defaultValue}${options}${extra}${placeholder} />`;
}
function checkbox(meta, defaultValue) {
    let model = meta['tabModel'];
    let options = getOptions(model);
    let extra = model.extra ? 'extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? 'placeholder="' + model.placeholder + '"' : "";
    return `<IvzCheckbox field="%s" title="%s"${defaultValue}${options}${extra}${placeholder} />`;
}
function slider(meta, defaultValue) {
    let model = meta['tabModel'];
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    let min = model.min ? ` :min="${model.min}"` : "";
    let max = model.max ? ` :max="${model.max}"` : "";
    let step = model.step ? ` :step="${model.step}"` : "";
    let range = model.range ? ` :range="${model.range}"` : "";
    let marks = model['isMarks'] ? ` :marks="${model['isMarks']}"` : "";
    // defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzSlider field="%s" title="%s"${step}${min}${max}${range}${marks}${extra}${placeholder} />`;
}
function select(meta, defaultValue) {
    let model = meta['tabModel'];
    let options = getOptions(model);
    let clear = model['clear'] ? ':allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    let mode = model['mode'] ? ` mode="${model['mode']}"` : '';
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let maxTagCount = model['maxTagCount'] ? ` :maxTagCount="${model['maxTagCount']}"` : '';
    return `<IvzSelect field="%s" title="%s"${defaultValue}${mode}${maxTagCount}${options}${clear}${extra}${placeholder} />`;
}
function rate(meta, defaultValue) {
    let model = meta['tabModel'];
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    let count = model.count ? ` :count="${model.count}"` : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let allowHalf = model['allowHalf'] ? ` :allowHalf="${model.allowHalf}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzRate field="%s" title="%s"${defaultValue}${count}${allowHalf}${extra}${placeholder} />`;
}
function number(meta, defaultValue) {
    let model = meta['tabModel'];
    let min = model.min ? ` :min="${model.min}"` : "";
    let max = model.max ? ` :max="${model.max}"` : "";
    let step = model.step ? ` :step="${model.step}"` : "";
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let precision = model['precision'] ? ` :precision="${model['precision']}"` : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzInputNumber field="%s" title="%s"${defaultValue}${step}${min}${max}${precision}${clear}${extra}${placeholder} />`;
}
function textarea(meta, defaultValue) {
    let model = meta['tabModel'];
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    let autoSize = model['autosize'];
    let autosize = autoSize[1] == 100 ? ` :autosize="true"` : ` :autosize="{minRows: ${autoSize[0]}, maxRows: ${autoSize[1]}"`
    return `<IvzTextarea field="%s" title="%s"${defaultValue}${autosize}${clear}${extra}${placeholder} />`;
}
function cascade(meta, defaultValue) {
    let model = meta['tabModel'];
    let options = getOptions(model);
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzCascader field="%s" title="%s"${defaultValue}${options}${clear}${extra}${placeholder} />`;
}
function stree(meta, defaultValue) {
    let model = meta['tabModel'];
    let options=getOptions(model);
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzTreeSelect field="%s" title="%s"${defaultValue}${options}${clear}${extra}${placeholder} />`;
}
function Switch(meta, defaultValue) {
    let model = meta['tabModel'];
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    let checkedChildren = ` checkedChildren=${model['checkedChildren']}`
    let unCheckedChildren = ` unCheckedChildren=${model['unCheckedChildren']}`

    let checkedValue = isBoolean(model['checkedValue']) ? ` :checkedValue="${model['checkedValue']}"` : ` checkedValue="${model['checkedValue']}"`
    let unCheckedValue = isBoolean(model['unCheckedValue']) ? ` :unCheckedValue="${model['unCheckedValue']}"` : ` checkedValue="${model['unCheckedValue']}"`
    return `<IvzSwitch field="%s" title="%s"${defaultValue}${checkedChildren}${unCheckedChildren}${checkedValue}${unCheckedValue}${extra}${placeholder} />`;
}
function date(meta, defaultValue) {
    let model = meta['tabModel'];
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultPickerValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    let format = model['format'] ? ` format="${model['format']}"` : '';
    let valueFormat = model['valueFormat'] ? ` valueFormat="${model['valueFormat']}"` : '';
    let showTime = model['showTime'] ? ' :showTime="true"' : '';
    let showToday = model['showToday'] ? ' :showToday="true"' : '';
    let dateType = model['dateType'];
    if(dateType == 'date') {
        return `<IvzDatePicker field="%s" title="%s"${defaultValue}${format}${valueFormat}${showTime}${showToday}${clear}${extra}${placeholder} />`;
    } else if(dateType == 'range') {
        return `<IvzRangePicker field="%s" title="%s"${defaultValue}${format}${valueFormat}${showTime}${showToday}${clear}${extra}${placeholder} />`;
    } else if(dateType == 'time') {
        return `<IvzTimePicker field="%s" title="%s"${defaultValue}${format}${valueFormat}${showTime}${showToday}${clear}${extra}${placeholder} />`;
    } else if(dateType == 'month') {
        return `<IvzMonthPicker field="%s" title="%s"${defaultValue}${format}${valueFormat}${showTime}${showToday}${clear}${extra}${placeholder} />`;
    } else {
        return `<IvzWeekPicker field="%s" title="%s"${defaultValue}${format}${valueFormat}${showTime}${showToday}${clear}${extra}${placeholder} />`;
    }
}
function password(meta, defaultValue) {
    let model = meta['tabModel'];
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    let visibilityToggle = model.visibilityToggle ? ` :visibilityToggle="true"` : '';
    return `<IvzInputPassword field="%s" title="%s"${defaultValue}${visibilityToggle}${clear}${extra}${placeholder} />`;
}
function mentions(meta, defaultValue) {
    let model = meta['tabModel'];
    let options = getOptions(model);
    let split = ` split="${model.split}"`;
    let prefix = ` prefix="${model.prefix}"`;
    let rows = model['rows'] > 1 ? ` rows="${model.rows}"` : '';
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzMentions field="%s" title="%s"${defaultValue}${split}${prefix}${rows}${options}${clear}${extra}${placeholder} />`;
}
function complete(meta, defaultValue) {
    let model = meta['tabModel'];
    let options = getOptions(model);
    let clear = model['clear'] ? ' :allowClear="true"' : '';
    let extra = model.extra ? ' extra="' + model.extra + '"' : "";
    defaultValue = defaultValue ? ` defaultValue="${defaultValue}"` : '';
    let placeholder = model.placeholder ? ' placeholder="' + model.placeholder + '"' : "";
    return `<IvzAutoComplete field="%s" title="%s"${defaultValue}${options}${clear}${extra}${placeholder} />`;
}

function foreign(meta, defaultValue) {
    let target = meta['target'];
    console.log("暂不支持外键模板")
}

function quote(meta, defaultValue) {
    let target = meta['tabModel']['target'];
    if(target) {
        let fn = ParserLib[target['type']];
        if(fn instanceof Function) {
            let cloneMeta = MetaUtils.cloneMeta(meta);
            cloneMeta['tabModel'] = target;
            return fn(cloneMeta, defaultValue);
        } else {
            console.error(`没有找到对应类型[${target['type']}]的ivzone解析函数`)
        }
    } else {
        console.warn("请选择对应的外键或者引用")
    }
}
const ParserLib = {text, rate, radio, number, cascade
    , complete, checkbox, select, slider, stree, foreign
    , password, date, mentions, switch: Switch, textarea, quote}

export function IvzoneParser() {

    /**
     * 解析编辑组件到ivzone组件
     * @param edit
     * @return {Object}
     */
    this.resolveEdit = function (edit) {
        let components = [];
        edit['metas'].filter(item => item['tabModel'].isForm).forEach(meta => {
            let component = ParserLib[meta.type];
            if(component) {
                let defaultValue = meta['tabModel'][meta.id];
                let template = component(meta, defaultValue);
                if(template) {
                    components.push({id: meta.id, template});
                }
            }
        });

        return components;
    }

    this.resolveSearch = function (edit, search) {
        let components = [], idMetaMaps = {};
        search['metas'].forEach(meta => {
            idMetaMaps[meta.id] = meta;
        })

        edit['metas'].filter(item => item['tabModel'].isSearch).forEach(meta => {
            let component = ParserLib[meta.type];
            if(component) {
                let defaultValue = idMetaMaps[meta.id]['tabModel'][meta.id];
                components.push({id: meta.id, template: component(meta, defaultValue)});
            }
        });
        return components;
    }

    this.resolveTable = function (table) {
        let components = [];
        table['metas'].forEach(meta => {
            let model = meta['tabModel'];
            let fixed = "", width = "";
            if(model['fixed']) {
                fixed = ", fixed: true";
            }

            if(model['width']) {
                width = `, width: ${model.width}`;
            }

            components.push({id: meta.id, template: `{field: "%s", title: "${model.title}", align: "${model.align}"${fixed}${width}}`})
        })

        return components;
    }
}